#include"Circle.h"

Circle::Circle(void) : Object()
{
	this->m_radius = 1;
	SetType(CIRCLE);
}
Circle::Circle(float posX, float posY, int m_radius) : Object(posX, posY)
{
	this->m_radius = m_radius;
	SetType(CIRCLE);
}
Circle::~Circle()
{
//	printf("Destructor Circle!!\n");
	
}
void Circle::Render()
{
	//DrawFullLine();
	VideoDriver::GetInstance()->DrawCircle(m_PositionX, m_PositionY, (float)m_radius);
}
int Circle::GetRadius()
{
	return this->m_radius;
}
void Circle::SetRadius(int m_radius)
{
	this->m_radius = m_radius;
}
void Circle::Update(float frameTime)
{
	int hasCollisionEdge = CheckCollisionWithEdge();
	if(CheckFlag(hasCollisionEdge, LEFT))
		cout<<"Collision LEFT"<<endl;
	else if(CheckFlag(hasCollisionEdge, RIGHT))
		cout<<"Collision RIGHT"<<endl;
	if(CheckFlag(hasCollisionEdge, TOP))
		cout<<"Collision TOP"<<endl;
	else if(CheckFlag(hasCollisionEdge, BOTTOM))
		cout<<"Collision BOTTOM"<<endl;
	
}
bool Circle::InRect(int posX, int posY)
{
	int distance = (int)sqrt(pow((m_PositionX - posX),2) + pow((m_PositionY - posY),2));
	if(distance < m_radius)
		return true;
	else return false;
}
//Task 3
int Circle::CheckCollisionWithEdge()
{
	int result = 0;
	if(this->m_PositionX <= this->m_radius)
		result |= LEFT;
	else if(this->m_PositionY >= (int)SCREEN_W - this->m_radius)
		return result |= RIGHT;
	if(this->m_PositionY <= this->m_radius)
		result |= TOP;
	else if(this->m_PositionY >= (int)SCREEN_H - this->m_radius)
		result |= BOTTOM;
	return result;

}
bool Circle::HasCollisionWithCircle(Circle *cirlce)
{
	float deltaX = this->m_PositionX - cirlce->m_PositionX;
	float deltaY = this->m_PositionY - cirlce->m_PositionY;
	float radiusCircle2 = cirlce->GetRadius();

	float sumRadius = radiusCircle2*radiusCircle2 + this->m_radius*this->m_radius;
	if(deltaX*deltaX + deltaY*deltaY < sumRadius*sumRadius)
		return true;

}
bool Circle::HasCollisionWithRectangle(Rectangles *rect)
{
	float posX , posY;
	posX = this->GetPosX();
	posY = this->GetPosY();
	if(posX < rect->GetPosX())
		posX = rect->GetPosX();
	else if(posX > rect->GetPosX() + rect->GetWidth())
		posX = rect->GetPosX() + rect->GetWidth();
	if(posY < rect->GetPosY())
		posY = rect->GetPosY();
	else if(posY > rect->GetPosY() + rect->GetHeight())
		posY = rect->GetPosY() + rect->GetHeight();
	float deltaX = posX - this->m_PositionX;
	float deltaY = posY - this->m_PositionY;
	if((deltaY*deltaY) + (deltaX*deltaX) < (this->m_radius*this->m_radius))
		return true;
	else return false;

}
bool Circle::HasCollisionWithObject(Object* object)
{
	int type = object->GetType();
	if(type == RECTANGLE)
		return HasCollisionWithRectangle((Rectangles*)object);
	else if(type == CIRCLE)
		return HasCollisionWithCircle((Circle*)object);
	else return false;
}
